<?php
/**
 * Created by PhpStorm.
 * User: 刘芳
 * Date: 2019/3/31
 * Time: 17:27
 */

namespace app\api\model;


class Product extends BaseModel
{
    protected $hidden = ['delete_time','update_time','id','img_id','banner_id','pivot','pivot'];

    public function getMainImgUrlAttr($value,$data)
    {
        return $this->_getUrlAttr($value,$data);
    }

    public function imgs()
    {
        return $this->hasMany('ProductImage', 'product_id', 'id');
    }

    public function properties()
    {
        return $this->hasMany('ProductProperty', 'product_id', 'id');
    }

    public static function getMostRecent($count)
    {
        $products = self::limit($count)->order("create_time desc") ->select();
        return $products;
    }
    public static function getProductsByCategoryID($categoryid)
    {
        $products = self::where('category_id','=',$categoryid)->select();
        return $products;
    }

    public static function getProductDetail($id)
    {
        $products = self::with(
                [
                    'imgs'=>function($query){
                        $query->with(['imgUrl'])
                            ->order('order','asc');
                    }
                ]
            )
            ->with(['properties'])
            ->find($id);
        return $products;
    }
}